/*
%include "/projects/hsieh_project/proj_201809/code_1_data/m_read.sas" /source2;

Macro that loads LBD data. 
It either 
1) reads the LBD data in projects/data_commons/ and runs data_0_read_raw.sas (slow), or 
2) reads the resulting data of the aforementioned process saved in ./data (fast).

param_dev = whether we are in development mode (which data set to read)
param_lyear = the list of years to keep
param_czone = whether to merge with czone
param_msa1983 = whether to merge with msa1983
param_msacz = whether to merge with msa*cz
param_drop = whether to drop additional industries
*/

libname pllbd "/projects/data_commons/lbd/";
libname plhr "/projects/hsieh_project/proj_201809/data/";

/*
================================================================================
*/

%macro m_read(param_dev=0, param_lyear=%quote(1977,2013), param_czone=0, param_msa1983=0, param_msacz=0, param_drop=1);

/*
--------------------------------------------------------------------------------
Load selected data
*/

%if &param_dev. = 0 %then %do;

%put --------------------------------------------------------------------------------;
%put Final Run;

data lbd;
  set pllbd.lbd_clean;
run;

/* Subset Years */
%if "&param_lyear." ~= "0" %then %do;
data lbd;
  set lbd;
  if year in (&param_lyear.);
run;
%end;

%include "/projects/hsieh_project/proj_201809/code_1_data/data_0_read_raw.sas" /source2;

%end;
%else %do;

%put --------------------------------------------------------------------------------;
%put Development;
%put &param_lyear.;

data lbd;
  set plhr.lbd_all;
run;

/* Subset Years */
%if "&param_lyear." ~= "0" %then %do;
data lbd;
  set lbd;
  if year in (&param_lyear.);
run;
%end;

%end;

/*
--------------------------------------------------------------------------------
Merge with commuting zones
*/

%if &param_czone. = 1 %then %do;

proc import out=cw_geo datafile="/projects/data_commons/cw/cw_geo_fips_czone_v12.dta" replace;
run;

proc sort data=lbd; by fips;
proc sort data=cw_geo; by fips;

data lbd;
  merge lbd(in=x) cw_geo;
  by fips;
  if x = 1;
run;

%end;

/*
--------------------------------------------------------------------------------
Merge with hybrid MSA1983
*/

%if &param_msa1983. = 1 %then %do;

proc import out=cw_geo(keep=fips msa1983) datafile="/projects/data_commons/cw/cw_geo_fips_msa1983_v12.dta" replace;
run;

proc sort data=lbd; by fips;
proc sort data=cw_geo; by fips;

data lbd;
  merge lbd(in=x) cw_geo;
  by fips;
  if x = 1;
run;

%end;

/*
--------------------------------------------------------------------------------
Merge with hybrid MSA-CZONE
*/

%if &param_msacz. = 1 %then %do;

proc import out=cw_geo datafile="/projects/data_commons/cw/cw_geo_fips_msa_czone_v12.dta" replace;
run;

proc sort data=lbd; by fips;
proc sort data=cw_geo; by fips;

data lbd;
  merge lbd(in=x) cw_geo;
  by fips;
  if x = 1;
run;

%end;

/*
REDACTED
Code related to dropping of a specific industry is redacted */



/*
--------------------------------------------------------------------------------
Prepare random variable
*/

data lbd;
  set lbd;
  runif=runif_1;
run;

%mend;

/* End of SAS file */